<?php

namespace api\admin\controller;


use api\mall\model\StoreAttrMapModel;
use api\mall\model\StoreAttrValueModel;
use api\mall\model\StoreAttrModel;
use cmf\controller\RestAdminBaseController;
use api\mall\model\ShoppingcardModel;

class AttrController extends RestAdminBaseController
{
    public function save(){
        $post = $this->request->post();
        $validate = [
            'attr_name'=>'require|length:2,6',
        ];
        $msgs = [
            'attr_name.require'=>'名称不能为空',
            'attr_name.length'=>'名称长度2-6',
        ];
        $result = $this->validate($post, $validate,$msgs);
        if($result!==true){
            $this->error($result,$post);
        }
        
        $data = ['item_id'=>$post['item_id'],'attr_name'=>$post['attr_name']];
        $model = new StoreAttrModel();
        if(!empty($post['id'])){
            $model->where('id',$post['id'])->update($data);
        }else{
            $num = $model->where('item_id',$data['item_id'])->count();
            if(empty($num)){
                $data['main'] = 1;
            }
            $model->insert($data);            
        }
        $this->success('保存成功');
    }

    public function delete(){
        $data = $this->request->route('id');
        if($data){
            $model = new StoreAttrModel();
            $item = $model->where('id',$data)->find();
            if($item){
                $model->where('id',$data)->delete();
                $vmodel = new StoreAttrValueModel();
                $vmodel->where('attr_id',$data)->delete();
                $this->updateMap($item['item_id']);
            }
        }
        $this->success('ok',$data);
    }

    public function value(){
        $model = new StoreAttrValueModel();
        $post = $this->request->post();
        $data = ['attr_value'=>$post['attr_value']];
        $validate = [
            'attr_value'=>'require|length:1,30',
        ];
        $msgs = [
            'attr_value.require'=>'名称不能为空',
            'attr_value.length'=>'名称长度1-30',
        ];
        $result = $this->validate($post, $validate,$msgs);
        if($result!==true){
            $this->error($result,$post);
        }
        
        if(empty($post['id'])){
            $data['item_id'] = $post['item_id'];
            $data['attr_id'] = $post['attr_id'];
            $model->insert($data);
            $this->updateMap($post['item_id']);
        }else{
            $model->where('id',$post['id'])->update($data);
        }        
        $this->success('保存成功');
    }
    
    private function updateMap($itemId){
        $attrModel = new StoreAttrModel();
//        $valueModel = new StoreAttrValueModel();
        $mapModel = new StoreAttrMapModel();
        $maps = $attrModel->alias('n')->join('store_attr_value v','n.id=v.attr_id')->where('n.main',1)->where('n.item_id',$itemId)->field('n.id,v.id vid')->select();
        $items = [];
        foreach ($maps as $item){
            $id = $item['id'];
            if(empty($items[$id])){
                $varr = [];
            }else{
                $varr = $items[$id];
            }
            $varr[] = $item['vid'];
            $items[$id] = $varr;
        }
        $count = count($items);
        if($count==0){
            return;
        }
        $itemsArr = [];
        foreach ($items as $varr){
            $itemsArr[] = $varr;
        }
        $vidlist = [];
        if($count==1){
            foreach ($itemsArr[0] as $v1){
                $vidlist[] = $v1;
            }
        }
        if($count==2){
            foreach ($itemsArr[0] as $v1){
                foreach ($itemsArr[1] as $v2){
                    $str = $v1>$v2?($v2.','.$v1):($v1.','.$v2);
                    $vidlist[] = $str;
                }
            }
        }
        if($count==3){
            foreach ($itemsArr[0] as $v1){
                foreach ($itemsArr[1] as $v2){
                    foreach ($itemsArr[2] as $v3){
                        $arr = [$v1,$v2,$v3];
                        sort($arr,SORT_NUMERIC);
                        $vidlist[] = $arr[0].','.$arr[1].','.$arr[2];
                    }
                }
            }
        }
        if($count==4){
            foreach ($itemsArr[0] as $v1){
                foreach ($itemsArr[1] as $v2){
                    foreach ($itemsArr[2] as $v3){
                        foreach ($itemsArr[3] as $v4){
                        $arr = [$v1,$v2,$v3,$v4];
                        sort($arr,SORT_NUMERIC);
                        $vidlist[] = $arr[0].','.$arr[1].','.$arr[2].','.$arr[3];
                        }
                    }
                }
            }
        }
        if($count==5){
            foreach ($itemsArr[0] as $v1){
                foreach ($itemsArr[1] as $v2){
                    foreach ($itemsArr[2] as $v3){
                        foreach ($itemsArr[3] as $v4){
                            foreach ($itemsArr[4] as $v5){
                        $arr = [$v1,$v2,$v3,$v4];
                        sort($arr,SORT_NUMERIC);
                        $vidlist[] = $arr[0].','.$arr[1].','.$arr[2].','.$arr[3].','.$arr[4];
                            }
                        }
                    }
                }
            }
        }
        $mapModel->where('item_id',$itemId)->whereNotIn('attr_value_id',$vidlist)->delete();
        $list = $mapModel->where('item_id',$itemId)->select();
        $insert = [];
        foreach ($vidlist as $new){
            $find = false;
            foreach ($list as $old){
                if($old['attr_value_id'] == $new){
                    $find = true;
                    break;
                }
            }
            if(!$find){
                $insert[] = [
                    'item_id'=>$itemId,'attr_value_id'=>$new,'price'=>0,'count'=>0
                ];
            }
        }
        if(!empty($insert)){
            $mapModel->insertAll($insert);
        }
    }

    
    public function map(){
        $model = new StoreAttrMapModel();
        $item_id=$this->request->post('item_id/s');
        $v_id=$this->request->post('id/d');
        $attr_name = $this->request->post('name/s');
        $value=$this->request->post('value/f');
        if($value<0){
            $this->error('数值不能小于0');
        }
        $old = $model->where('item_id',$item_id)->where('id',$v_id)->find();
        if($old){
            $model->where('id',$old['id'])->update([$attr_name=>$value]);
        }else{
//            $data = [
//                'item_id'=>$item_id,
//                'attr_value_id'=>$v_id,
//                'price'=>0,
//                'count'=>0,
//                ];
//            $data[$attr_name]=$value;
//            $model->insert($data);
            $this->error('属性不存在');
        }
        //如果修改价格,重置购物车价格
        if($old && $attr_name=='price'){
            $spmode = new ShoppingcardModel();
            $spmode->where('item_id',$item_id)->where('sub_id',$old['id'])->update(['price'=>$value]);
        }
        //所有价格中的最小值
        $price = $model->where('item_id',$item_id)->where('price','>',0)->order('price','asc')->find();
        $item = new \api\mall\model\StoreModel();
        if($price){
            $item->where('id',$item_id)->update(['item_price'=>$price->price]);
        }
        if($attr_name=='count'){
            $min = $model->where('item_id',$item_id)->where('count','>',0)->min('count');
            if($min==0||$value==0){
                $min=$value;
            }
            $item->where('id',$item_id)->update(['item_num'=>$min]);
        }
//        $attrModel = new \api\mall\model\StoreAttrModel();
//        $main_attr = $attrModel->where('item_id',$item_id)->where('main',1)->find();
//        if($main_attr){
//            $count = $model->alias('map')->join('store_attr_value','store_attr_value.id=map.attr_value_id')->where('map.item_id',$item_id)->where('store_attr_value.attr_id',$main_attr->id)->sum('count');
//            $item->where('id',$item_id)->update(['item_num'=>$count]);
//        }
        $this->success('ok');
    }
    
    public function change_main($id,$type){
        $model = new StoreAttrModel();
        $attr = $model->where('id',$id)->find();
        if(empty($attr)){
            $this->error('系统异常');
        }
        $item_id = $attr->item_id;
        $num = $model->where('item_id',$item_id)->where('main',1)->count();
        if($type==1){
//            if($num>2){
//                $this->error('最多三个规格');
//            }
        }else{
            if($num<2){
                $this->error('最少保留一个规格');
            }
        }
        $model->where('id',$id)->update(['main'=>$type]);
        //设置规格,清空所有规格设置
        $map = new StoreAttrMapModel();
        $map->where('item_id',$item_id)->delete();
        $this->updateMap($item_id);
        $this->success('ok');
    }

}